package com.cupcorn.balapkarung.entity;

import java.util.ArrayList;

import org.andengine.extension.tmx.TMXObject;
import org.andengine.extension.tmx.TMXObjectGroup;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.util.color.Color;

import com.cupcorn.lib.Anchor;
import com.cupcorn.lib.Ratio;
import com.cupcorn.lib.display.DisplayRectangle;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameRectangle;
import com.cupcorn.lib.game.element.GameElementRectangle;

public class Map
{
	GameEngine						engine;
	TMXTiledMap						map;
	MC								mc;
	
	ArrayList<GameElementRectangle>	listElementGround	= new ArrayList<GameElementRectangle>();
	DisplayRectangle				displayGround;
	public GameRectangle[]			ground;
	
	ArrayList<GameElementRectangle>	listElementGameOver	= new ArrayList<GameElementRectangle>();
	DisplayRectangle				displayGameOver;
	public GameRectangle[]			gameOver;
	
	public Map(GameEngine engine, TMXTiledMap map, MC mc)
	{
		this.engine = engine;
		this.map = map;
		this.mc = mc;
		
		loadObjectMap();
	}
	
	private void loadObjectMap()
	{
		for (TMXObjectGroup group : map.getTMXObjectGroups())
		{
			if (group.getName().equals("P1")) for (TMXObject object : group.getTMXObjects())
				mc.mc.setPosition(object.getX(), object.getY());
			
			if (group.getName().equals("ground"))
				for (TMXObject object : group.getTMXObjects())
					listElementGround.add(new GameElementRectangle(object.getX(), object.getY(), object.getWidth(), object.getHeight(), Color.WHITE, 0,
						Anchor.TOP_LEFT, Ratio.NONE));
			
			if (group.getName().equals("gameOver"))
				for (TMXObject object : group.getTMXObjects())
					listElementGameOver.add(new GameElementRectangle(object.getX(), object.getY(), object.getWidth(), object.getHeight(), Color.WHITE, 0,
						Anchor.TOP_LEFT, Ratio.NONE));
		}
		
		displayGround = new DisplayRectangle(engine, listElementGround);
		ground = new GameRectangle[listElementGround.size()];
		for (int i = 0; i < listElementGround.size(); i++)
		{
			ground[i] = displayGround.newRectangle(i);
		}
		
		displayGameOver = new DisplayRectangle(engine, listElementGameOver);
		gameOver = new GameRectangle[listElementGameOver.size()];
		for (int i = 0; i < listElementGameOver.size(); i++)
		{
			gameOver[i] = displayGameOver.newRectangle(i);
		}
	}
}
